package com.ruoyi.project.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SmPay;
import com.ruoyi.project.system.domain.SmQuery;
import com.ruoyi.project.system.domain.vo.QueryVo;
import com.ruoyi.project.system.mapper.SmQueryMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISmPayService;
import com.ruoyi.project.system.service.ISmProductService;
import com.ruoyi.project.system.service.ISmQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
import static com.ruoyi.framework.web.domain.AjaxResult.error;
import static com.ruoyi.framework.web.domain.AjaxResult.success;

/**
 * 查询进件Service业务层处理
 */
@Service
public class SmQueryServiceImpl implements ISmQueryService {

    @Autowired
    private ISmProductService smProductService;

    @Autowired
    private ISmPayService smPayService;

    @Autowired
    private SmQueryMapper smQueryMapper;

    @Autowired
    private SysUserMapper userMapper;

    public static String[] paths = {"bdp", "bd", "com", "check", "jud", "imm"};

    @Override
    @Transactional
    public AjaxResult selfQueryPay(SmQuery smQuery) {
        // 1. 构建支付订单
        Integer userId = smQuery.getUserId();
        Integer productId = smQuery.getProductId();
        Integer roleId = userMapper.queryRoleId(userId.longValue());
        BigDecimal pricing = smProductService.basePrice(userId.longValue(), productId, roleId);
        SmPay smPay = new SmPay(smQuery.getId(), pricing, userId, smQuery.getRemark(),"自用查询");
        // 2. 写入支付订单
        smPayService.insertSmPay(smPay);
        // 3. 计算写入收益
        smPayService.calculateCommonIncome(smPay, productId, roleId);
        // 4. 发起支付
        try {
            JSONObject payInfo = smPayService.zhifuFM(smPay, paths[productId]);
            payInfo.put("orderNo", smPay.getOrderNo());
            return success(payInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return error("发起支付异常！");
        }
    }

    @Override
    @Transactional
    public AjaxResult promotionQueryPay(SmQuery smQuery) {
        // 1. 构建支付订单
        Integer userId = smQuery.getUserId();
        Integer productId = smQuery.getProductId();
        Integer roleId = userMapper.queryRoleId(userId.longValue());
        BigDecimal pricing = smProductService.promotionPrice(userId.longValue(), productId, roleId);
        SmPay smPay = new SmPay(smQuery.getId(), pricing, userId, smQuery.getRemark(),"他人查询");
        // 2. 写入支付订单
        smPayService.insertSmPay(smPay);
        // 3. 计算写入收益
        smPayService.calculatePromotionIncome(smPay, productId, roleId);
        // 4. 发起支付
        try {
            JSONObject payInfo = smPayService.zhifuFM(smPay, paths[productId]);
            payInfo.put("orderNo", smPay.getOrderNo());
            return success(payInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return error("发起支付异常！");
        }
    }

    @Override
    public AjaxResult judicialQuery(SmQuery smQuery) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        params.put("req_time", System.currentTimeMillis());
        params.put("request_sn", "XCN" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase() + "JF025");
        params.put("inter_id", "JF025");
        params.put("name", smQuery.getName());
        params.put("cardNo", smQuery.getCardNo());
        result.put("grss", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        return result;
    }

    @Override
    public AjaxResult bigDataQuery(SmQuery smQuery) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        params.put("req_time", System.currentTimeMillis());
        params.put("request_sn", "XCN" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase() + "JF011");
        params.put("inter_id", "JF011");
        params.put("name", smQuery.getName());
        params.put("cardNo", smQuery.getCardNo());
        params.put("mobile", smQuery.getTel());
        result.put("dttz", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        return result;
    }

    @Override
    public AjaxResult bigDataQueryPro(SmQuery smQuery) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        String baseReqSn = "XCN" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase();
        params.put("req_time", System.currentTimeMillis());
        // 个人诉讼
        params.put("request_sn", baseReqSn + "JF025");
        params.put("inter_id", "JF025");
        params.put("name", smQuery.getName());
        params.put("cardNo", smQuery.getCardNo());
        result.put("grss", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 手机三要素
        params.replace("request_sn", baseReqSn + "JF007");
        params.replace("inter_id", "JF007");
        params.put("mobile", smQuery.getTel());
        result.put("sjsys", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 多头
        params.replace("request_sn", baseReqSn + "JF011");
        params.replace("inter_id", "JF011");
        result.put("dttz", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 借选指数
        params.replace("request_sn", baseReqSn + "JF010");
        params.replace("inter_id", "JF010");
        result.put("jxzs", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 特殊名单验证
//        String[] labelsList = sysBlacklistMapper.selectLabelsList(new SysBlacklist(smQuery));
        JSONArray includes = new JSONArray();
//        for (String labels : labelsList) {
//            JSONArray array = JSON.parseArray(labels);
//            for (int i = 0; i < array.size(); i++) {
//                String label = array.getString(i);
//                if (!includes.contains(label)) {
//                    includes.add(label);
//                }
//            }
//        }
        JSONObject labels = new JSONObject();
        labels.put("retcode", "000000");
        labels.put("retdata", includes.toJSONString());
        result.put("labels", labels);
        return result;
    }

    @Override
    public JSONObject companyQuery(SmQuery smQuery) {
        JSONObject params = new JSONObject();
        String baseReqSn = "XCN" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase();
        params.put("req_time", System.currentTimeMillis());
        // 个人诉讼
        params.put("request_sn", baseReqSn + "JF033");
        params.put("inter_id", "JF033");
        params.put("mobile", smQuery.getTel());
        return JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params));
    }

    @Override
    public AjaxResult marryQuery(SmQuery smQuery, String orderNo) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        String baseReqSn = "XCN" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase();
        params.put("req_time", System.currentTimeMillis());
        // 实名认证
        params.put("request_sn", baseReqSn + "JF005");
        params.put("inter_id", "JF005");
        params.put("name", smQuery.getName());
        params.put("cardNo", smQuery.getCardNo());
        result.put("smrz", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 个人诉讼
        params.replace("request_sn", baseReqSn + "JF025");
        params.replace("inter_id", "JF025");
        result.put("grss", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 婚姻
//        params.put("mobile", smQuery.getMobile());
        params.replace("request_sn", baseReqSn + "JF032");
        params.replace("inter_id", "JF032");
        result.put("hyhy", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));

//        SMSUtils.sendReport(smQuery.getTel(), "mar", orderNo);

        return result;
    }

//    @Override
//    public List<Record> getRecords(String mobile, String sign) {
//        List<Record> recordList = smQueryMapper.recordList(mobile, sign);
//        // 处理数据
//        for (Record record : recordList) {
//            if (record.getProductId() == 3) {         // 企业 MK
//                String m = record.getMobile();
//                String mk = record.getMoreKeyword();
//                record.setMoreKeyword(StringUtils.isEmpty(mk) ? m : mk);
//            } else {
//                record.setMoreKeyword(StringUtils.isEmpty(record.getName()) ? "查询关键字不全" : ("**" + record.getName().substring(1)));
//            }
//            record.setPayState("SUCCESS".equals(record.getPayState()) ? "-查询完成" : "");
//            record.setName(null);
//            record.setCardNo(null);
//            record.setMobile(null);
//        }
//        return recordList;
//    }


    /**
     * 查询查询进件
     *
     * @param id 查询进件主键
     * @return 查询进件
     */
    @Override
    public SmQuery selectSmQueryById(Integer id) {
        return smQueryMapper.selectSmQueryById(id);
    }

    /**
     * 查询查询进件列表
     *
     * @param queryVo 查询进件
     * @return 查询进件
     */
    @Override
    public List<QueryVo> selectSmQueryList(QueryVo queryVo) {
        return smQueryMapper.selectSmQueryList(queryVo);
    }

    /**
     * 新增查询进件
     *
     * @param smQuery 查询进件
     * @return 结果
     */
    @Override
    public int insertSmQuery(SmQuery smQuery) {
        return smQueryMapper.insertSmQuery(smQuery);
    }

    /**
     * 修改查询进件
     *
     * @param smQuery 查询进件
     * @return 结果
     */
    @Override
    public int updateSmQuery(SmQuery smQuery) {
        smQuery.setUpdateTime(DateUtils.getNowDate());
        return smQueryMapper.updateSmQuery(smQuery);
    }

    /**
     * 批量删除查询进件
     *
     * @param ids 需要删除的查询进件主键
     * @return 结果
     */
    @Override
    public int deleteSmQueryByIds(Integer[] ids) {
        return smQueryMapper.deleteSmQueryByIds(ids);
    }

}
